package com.wisdudu.blank.ui.user;

import android.content.Intent;
import android.databinding.ObservableField;
import android.text.TextUtils;

import com.kelin.mvvmlight.base.ViewModel;
import com.kelin.mvvmlight.command.ReplyCommand;
import com.wisdudu.blank.data.Injection;
import com.wisdudu.blank.data.repo.UserRepo;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.ProgressSubscriber;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.SubscriberOnNextErrorListener;
import com.wisdudu.blank.support.util.AppUtil;
import com.wisdudu.blank.support.util.ToastUtil;
import com.wisdudu.blank.support.widget.loading.LoadingProgressDialog;
import com.wisdudu.blank.ui.login.LoginActivity;
import com.wisdudu.frame.support.util.RegularUtil;


/**
 * Created by ypp on 2016/10/8.
 */
public class ModifyPassVM implements ViewModel {
    private ModifyPassFragment fragment;
    public ObservableField<String> password = new ObservableField<>();
    public ObservableField<String> newPassword = new ObservableField<>();
    public ObservableField<String> reNewPassword = new ObservableField<>();
    private UserRepo mUserRepo;

    public ModifyPassVM(ModifyPassFragment modifyPassFragment) {
        this.fragment = modifyPassFragment;
        mUserRepo = Injection.provideUserRepo();
    }

    public ReplyCommand confirm = new ReplyCommand(() -> {
        fragment.hideKeyboard();
        if (TextUtils.isEmpty(password.get())) {
            ToastUtil.INSTANCE.toast("请输入原密码");
            return;
        }
        if (RegularUtil.isNumber(password.get())) {
            ToastUtil.INSTANCE.toast("密码全为数字，安全等级较低，请重新设置");
            return;
        }
        if (!RegularUtil.isPassword(password.get())) {
            ToastUtil.INSTANCE.toast("请输入6-20位字母或数字密码");
            return;
        }
        if (TextUtils.isEmpty(newPassword.get())) {
            ToastUtil.INSTANCE.toast("请输入新密码");
            return;
        }
        if (RegularUtil.isNumber(newPassword.get())) {
            ToastUtil.INSTANCE.toast("密码全为数字，安全等级较低，请重新设置");
            return;
        }
        if (!RegularUtil.isPassword(newPassword.get())) {
            ToastUtil.INSTANCE.toast("请输入6-20位字母或数字密码");
            return;
        }
        if (TextUtils.isEmpty(reNewPassword.get())) {
            ToastUtil.INSTANCE.toast("请输入重复的密码");
            return;
        }
        if (!reNewPassword.get().equals(newPassword.get())) {
            ToastUtil.INSTANCE.toast("新密码两次输入不一致");
            return;
        }
        if (password.get().equals(reNewPassword.get())) {
            ToastUtil.INSTANCE.toast("新密码与原密码不能一致！");
            return;
        }
        mUserRepo.editPass(password.get(), reNewPassword.get())
                .compose(fragment.bindToLifecycle())
                .subscribe(
                        new ProgressSubscriber<Object>(new SubscriberOnNextErrorListener<Object>() {
                            @Override
                            public void onError(Throwable e, LoadingProgressDialog pd) {
                                ToastUtil.INSTANCE.netError();
                            }

                            @Override
                            public void onNext(Object abs, LoadingProgressDialog pd) {
                                ToastUtil.INSTANCE.toast("修改成功");
                                logout();
                            }
                        }, fragment.getActivity(), "正在修改..."));
    });

    private void logout() {
        AppUtil.jumpActivity(fragment.getActivity(), LoginActivity.class);
        mUserRepo.logout();
        Intent intent = new Intent();
        intent.setAction("finishActivity");
        fragment.getActivity().sendBroadcast(intent);
        fragment.getActivity().finish();
    }
}
